<?php

class Images extends CI_Controller {
    
    public $_updir = 'public/template/backend/images/products/';
    private $_updat;
    private $_auth;
    public function __construct() {
        parent::__construct();
        $this->_auth = $this->myclass->checkuser();
        if($this->_auth == FALSE) redirect ("backend/home");
    }
    
    public function view($id){
        $data['data']['parcat']= $this->mimages->getByProId($id,TRUE);
        $data['data']['pro_id']= $id;
        $data['title'] = "Hình ảnh thêm";
        $data['temp'] = "backend/images/home";
        $this->load->view("backend/layout/index",$data);
    }
    
    public function add($id) {
        $img = null;
        if ($this->input->post("add")) {    
            $post = $this->input->post("data");
            $config['upload_path'] = $this->_updir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('img')) {
                $this->_updat = $this->upload->data();
                $post['img'] = $this->_updir . $this->_updat['file_name'];
                $post['pro_id'] = $id;
                $this->mimages->insert($post);
                redirect("backend/images/view" . "/" . $id);
            } else {
                $data['data'] = array('error' => $this->upload->display_errors());              
            }     
        }
        $data['data']['pro_id'] = $id;
        $data['data']['color'] = $this->mcolor->getByProId($id);
        $data['title'] = "Thêm ảnh";
        $data['temp'] = "backend/images/form";
        $this->load->view("backend/layout/index", $data);
    }
    
    public function update($id) {
        $data['data']['img'] = $this->mimages->fetchOne($id);
        if ($this->input->post("add")) {   
            $post = $this->input->post("data");
            $config['upload_path'] = $this->_updir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('img')) {
                $this->_updat = $this->upload->data();
                $post['img'] = $this->_updir . $this->_updat['file_name'];
            } else {
                $post['img'] = $data['data']['img']->img;
            }
            $this->mimages->update($id, $post);
            redirect("backend/images/view" . "/" . $data['data']['img']->pro_id);
        }
        $data['data']['pro_id'] = $data['data']['img']->pro_id;
        $data['data']['color'] = $this->mcolor->getByProId($data['data']['img']->pro_id);
        $data['title'] = "Sửa ảnh";
        $data['temp'] = "backend/images/form";
        $this->load->view("backend/layout/index", $data);
    }
    
    public function delete($id,$pro_id){
        $this->mimages->delete($id);
        redirect("backend/images/view" . "/" . $pro_id);
    }
    
    public function deleteall($id){
        $this->mimages->deleteAll($id);
        redirect("backend/images/view" . "/" . $id);
    }

}
